package Screens.MyScreens;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

import GUI.GuiManager;
import Game.Level;
import Game.Statistics;
import Game.Spawners.SpawnAltar;
import Game.Spawners.SpawnAnvil;
import Game.Spawners.SpawnBarrels;
import Game.Spawners.SpawnChest;
import Game.Spawners.SpawnGoldChest;
import Game.Spawners.SpawnDoors;
import Game.Spawners.SpawnLever;
import Game.Spawners.SpawnMobs;
import Game.Spawners.SpawnPlayer;
import Game.Spawners.SpawnSeller;
import Game.Spawners.SpawnTorches;
import Game.Spawners.SpawnTrapdoor;
import Game.Spawners.SpawnerBase;
import Screens.Manager.Screen;
import Screens.Manager.ScreenManager;
import Tools.Configuration;
import Tools.ResourceManager;
import Tools.Drawing.FontTagSystem;

/** Game screen class
 * @author Aikidos
 */
public class GameScreen extends Screen {
	/** Level */
	private Level _level;
	
	/** Default list of spawner */
	private List<SpawnerBase> _lstSpawners = new ArrayList<SpawnerBase>();
	
	/** Floor */
	private int _floor;
	
	/** Returns floor 
	 * @return Floor
	 */
	public int getFloor() {
		return _floor;
	}
	
	/** Returns default list of spawner
	 * @return Default list of spawner
	 */
	public List<SpawnerBase> getDefaultSpawners() {
		return _lstSpawners;
	}
	
	/** Set floor
	 * @param floor - Floor
	 */
	public void setFloor(int floor) {
		_floor = floor;
	}
	
	@Override
	public void init() throws SlickException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		// Stop main theme music
		if (!Configuration.isMute())
			((Music)ResourceManager.getPool().get("music_MainTheme")).stop();
		
		// Initialize game statistics
		Statistics.init();
		
		// Initialize default spawn list
		_lstSpawners = new ArrayList<SpawnerBase>();
		_lstSpawners.add(new SpawnPlayer());
		_lstSpawners.add(new SpawnLever());
		_lstSpawners.add(new SpawnTrapdoor());
		_lstSpawners.add(new SpawnMobs());
		_lstSpawners.add(new SpawnGoldChest());
		_lstSpawners.add(new SpawnAltar());
		_lstSpawners.add(new SpawnTorches());
		_lstSpawners.add(new SpawnDoors());
		_lstSpawners.add(new SpawnBarrels());
		_lstSpawners.add(new SpawnAnvil());
		_lstSpawners.add(new SpawnSeller());
		_lstSpawners.add(new SpawnChest());
		
		_level = new Level(this);
		
		_level.init();
		_level.create(_floor, _lstSpawners);
		
		// Initialize GUI
		GuiManager.init(this, _level);
		
		// Initialize tag system
		FontTagSystem.init();
		
		UnicodeFont tagRed = new UnicodeFont("res/fonts/mainfont.ttf", 11, false, false);
		tagRed.addAsciiGlyphs();
		tagRed.getEffects().add(new ColorEffect(java.awt.Color.RED));
		tagRed.loadGlyphs();
		
		UnicodeFont tagGreen = new UnicodeFont("res/fonts/mainfont.ttf", 11, false, false);
		tagGreen.addAsciiGlyphs();
		tagGreen.getEffects().add(new ColorEffect(java.awt.Color.GREEN));
		tagGreen.loadGlyphs();
		
		UnicodeFont tagYellow = new UnicodeFont("res/fonts/mainfont.ttf", 11, false, false);
		tagYellow.addAsciiGlyphs();
		tagYellow.getEffects().add(new ColorEffect(java.awt.Color.YELLOW));
		tagYellow.loadGlyphs();
		
		UnicodeFont tagCyan = new UnicodeFont("res/fonts/mainfont.ttf", 10, false, false);
		tagCyan.addAsciiGlyphs();
		tagCyan.getEffects().add(new ColorEffect(java.awt.Color.CYAN));
		tagCyan.loadGlyphs();
		
		FontTagSystem.addTag("red", tagRed);
		FontTagSystem.addTag("green", tagGreen);
		FontTagSystem.addTag("yellow", tagYellow);
		FontTagSystem.addTag("cyan", tagCyan);
	}

	@Override
	public void loadContent() {
		
		try {
			// Load images
			ResourceManager.getPool().put("gfx_floor", ResourceManager.LoadImage("res/gfx/floor.png"));
			ResourceManager.getPool().put("gfx_wall", ResourceManager.LoadImage("res/gfx/wall.png"));
			
			// Load sounds
			if (!Configuration.isMute()) {
				ResourceManager.getPool().put("sfx_equip", new Sound("res/sfx/equip.wav"));
				ResourceManager.getPool().put("sfx_door_lock", new Sound("res/sfx/door_lock.wav"));
				ResourceManager.getPool().put("sfx_door_unlock", new Sound("res/sfx/door_unlock.wav"));
				ResourceManager.getPool().put("sfx_meat", new Sound("res/sfx/meat.wav"));
				ResourceManager.getPool().put("sfx_drink", new Sound("res/sfx/drink.wav"));
				ResourceManager.getPool().put("sfx_keys", new Sound("res/sfx/keys.wav"));
				ResourceManager.getPool().put("sfx_barrel", new Sound("res/sfx/barrel.wav"));
				ResourceManager.getPool().put("sfx_chest", new Sound("res/sfx/chest.wav"));
				ResourceManager.getPool().put("sfx_anvil", new Sound("res/sfx/anvil.wav"));
				ResourceManager.getPool().put("sfx_lever", new Sound("res/sfx/lever.wav"));
				ResourceManager.getPool().put("sfx_level_up", new Sound("res/sfx/level_up.wav"));
				ResourceManager.getPool().put("sfx_skill", new Sound("res/sfx/skill.wav"));
				ResourceManager.getPool().put("sfx_buy", new Sound("res/sfx/buy.wav"));
			}
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(int delta) throws SlickException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
		_level.update(delta);
		if (_level.getObjectManager().getPlayer().isDied())
			ScreenManager.go("DeadScreen");
		
		GuiManager.update(delta);
	}

	@Override
	public void draw(Graphics g) throws SlickException {
		_level.draw(g);
		
		// Draw GUI
		GuiManager.draw(g);
	}

	@Override
	public void unloadContent() {
		ResourceManager.getPool().remove("gfx_floor");
		ResourceManager.getPool().remove("gfx_wall");
		
		if (!Configuration.isMute()) {
			ResourceManager.getPool().remove("sfx_equip");
			ResourceManager.getPool().remove("sfx_door_close");
			ResourceManager.getPool().remove("sfx_meat");
			ResourceManager.getPool().remove("sfx_door_unlock");
			ResourceManager.getPool().remove("sfx_drink");
			ResourceManager.getPool().remove("sfx_keys");
			ResourceManager.getPool().remove("sfx_barrel");
			ResourceManager.getPool().remove("sfx_chest");
			ResourceManager.getPool().remove("sfx_anvil");
			ResourceManager.getPool().remove("sfx_lever");
			ResourceManager.getPool().remove("sfx_level_up");
			ResourceManager.getPool().remove("sfx_skill");
			ResourceManager.getPool().remove("sfx_buy");
		}
	}

	@Override
	public void shutdown() {
		_floor = 0;
		
		GuiManager.deInit();
		Statistics.deInit();
	}

}
